package com.arpa.wly.waybill;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopBaseHttpResponse;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.*;
import com.pdd.pop.sdk.http.api.pop.response.*;
import org.junit.Test;

/**
 * @author liuxr
 * @date 2021年08月17日 16:46
 */
public class PddWaybill {
    String code = "4eb7fe7d-1fe3-4e70-b0c4-d4e40557516d";


    String clientId = "24f4f08553284648b4653ce008345514";
    String clientSecret = "3d1269b8a012d499c90eaa59940ca6e2328795da";
    String accessToken = "bebf6a0aaa2b4533a9fae9fb5a4bd8c1eeb83b1d";
    String refreshToken = "16a4ac4d25bb401eb2afb13765af7a872a1163b9";


    /**
     * 获取token
     * @author liuxr
     * @date 2021/8/23 9:10
     */
    @Test
    public void getToken(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddPopAuthTokenCreateRequest request = new PddPopAuthTokenCreateRequest();
        request.setCode("3a1ab3be6ba444d393f56178186c4f2a4794e740");
        PddPopAuthTokenCreateResponse response = null;
        try {
            response = client.syncInvoke(request);
        } catch (Exception e) {
            e.printStackTrace();
        }

        PopBaseHttpResponse.ErrorResponse  errorResponse = response.getErrorResponse();
        if(errorResponse!=null){
            System.out.println(errorResponse.getErrorMsg() + ":" + errorResponse.getSubMsg());
        }

        System.out.println(JsonUtil.transferToJson(response));
    }
    @Test
    public void refreshToken(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddPopAuthTokenRefreshRequest request = new PddPopAuthTokenRefreshRequest();
        request.setRefreshToken(refreshToken);
        PddPopAuthTokenRefreshResponse response = null;
        try {
            response = client.syncInvoke(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("失效日期："+ DateUtil.offset(DateUtil.date(), DateField.SECOND, response.getPopAuthTokenRefreshResponse().getExpiresIn()));
        System.out.println(JsonUtil.transferToJson(response));
    }
    /**
     * 返回打印 params参数
     * access_token=值&app_key=值&method=logistics.getShopKey&param_json={}&timestamp=时间&v=2&sign=值，
     * 此处只用关心入参，不用关心出参，这个是打印组件调用的，各参数值的生成详见电商开放平台https://op.jinritemai.com/docs/guide-docs/10/23，其中的method=logistics.getShopKey，param_json={}
     * @author liuxr
     * @date 2021/8/17 16:34
     */
    @Test
    public void getPrintParam(){
    }



    @Test
    public void customares(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddCloudprintCustomaresGetRequest request = new PddCloudprintCustomaresGetRequest();
        request.setTemplateId(1);
        PddCloudprintCustomaresGetResponse response = null;
        try {
            response = client.syncInvoke(request, accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JsonUtil.transferToJson(response));
    }

    /**
     * 拼多多标准模板
     * @author liuxr
     * @date 2021/10/22 14:33
     */
    @Test
    public void stdtemplates(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddCloudprintStdtemplatesGetRequest request = new PddCloudprintStdtemplatesGetRequest();
        request.setWpCode("ZTO");
        PddCloudprintStdtemplatesGetResponse response = null;
        try {
            response = client.syncInvoke(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JsonUtil.transferToJson(response));
    }

    /**
     * 通过运单号获取打印信息
     * @author liuxr
     * @date 2021/8/17 16:32
     */
    @Test
    public void waybillApply(){
    }

    /**
     * 查询商家和物流商的订购关系以及物流单号使用情况
     * @author liuxr
     * @date 2021/8/17 16:33
     */
    @Test
    public void search(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddWaybillSearchRequest request = new PddWaybillSearchRequest();
        request.setWpCode("YUNDA");
        PddWaybillSearchResponse response = null;
        try {
            response = client.syncInvoke(request, accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JsonUtil.transferToJson(response));
    }

    /**
     * 获取电子面单号
     * @author liuxr
     * @date 2021/8/17 16:33
     */
    @Test
    public void newCreateOrder(){
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddWaybillSearchRequest request = new PddWaybillSearchRequest();
        request.setWpCode("ZTO");
        PddWaybillSearchResponse response = null;
        try {
            response = client.syncInvoke(request, accessToken);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JsonUtil.transferToJson(response));
    }



}
